package com.accp.restaurant.service;

import com.accp.restaurant.entity.Dishes;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;
@Service
public class DishesService {

    @PersistenceContext
    private EntityManager entityManager;

    @CachePut(value = "dishes",key = "#disheslabelid")
    public List<Dishes> getOrderdishes(Long disheslabelid, String dishesname,Long roomId){
        String jpql = "select d from Dishes d join fetch d.disheslabel where 1=1";

        if(disheslabelid!=0){
            jpql +=" and d.disheslabel.disheslabelid=:disheslabelid";
        }
        if(dishesname!=null&&!dishesname.equals("")){
            jpql +=" and d.dishesname like :dishesname";
        }
        Query query = entityManager.createQuery(jpql);
        if(disheslabelid!=0){
            query.setParameter("disheslabelid",disheslabelid);
        }
        if(dishesname!=null&&!dishesname.equals("")){
            query.setParameter("dishesname","%"+dishesname+"%");
        }
        List<Dishes> dishes = query.getResultList();
        return dishes;
    }





}
